package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class TranslationActivity extends AppCompatActivity {

    // 展示列表对象
    private  LinearLayout contentView;
    // 记录选中的翻译选项下标
    private  Integer index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translation);

        // 获取信息展示列表对象
        contentView = findViewById(R.id.content);

        // 获取下拉列表对象
        Spinner spinner = (Spinner) findViewById(R.id.transItem);
        //下拉数据列表
        List<String> data_list = new ArrayList<String>();
        data_list.add("英文");
        data_list.add("广东话");
        data_list.add("俄语");
        data_list.add("法语");

        List<String> dataVals = new ArrayList<>();
        dataVals.add("en");
        dataVals.add("yue");
        dataVals.add("ru");
        dataVals.add("fr");

        //适配器
        ArrayAdapter arr_adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner.setAdapter(arr_adapter);
        // 给spinner添加点击选择监听器
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                index = i;
                Toast.makeText(TranslationActivity.this,"翻译成："+dataVals.get(i),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });




        // 获取翻译对像
        LinearLayout transBtn = findViewById(R.id.transLL);
        // 获取要翻译的内容对象
        EditText transContET = findViewById(R.id.transCont);

        // 给翻译按钮添加点击监听事件
        transBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取输入框内容
                String content = transContET.getText().toString();
                if (content.isEmpty()) {
                    Toast.makeText(TranslationActivity.this, "要翻译的内容不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 将获取到的内容显示在界面上
                setRightMsg(content);

                // 把内容传给讯飞后台翻译
                //翻译
                WebITS.ResponseData responseData = WebITS.translate(dataVals.get(index),content);
                //翻译结果
                String result = "";
                if (responseData != null && responseData.getData() != null) {
                    result = responseData.getData().getResult().getTrans_result().getDist().toString();
                }
                // 将翻译的结果显示在左侧
                setLeftMsg(result);

                // 清空输入框
                transContET.setText("");
            }
        });

    }

    /**
     * 设置右边聊天内容
     * @param msg
     */
    private  void setRightMsg(String msg){
        //构建右边聊天
        View rightView = LayoutInflater.from(TranslationActivity.this).inflate(R.layout.right_talk_layout, contentView, false);
        TextView rightTV = rightView.findViewById(R.id.rightTV);
        rightTV.setText(msg);
        contentView.addView(rightView);
    }

    /**
     * 设置左边聊天内容
     * @param msg
     */
    private  void setLeftMsg(String msg) {
        //构建左边聊天
        View leftView = LayoutInflater.from(TranslationActivity.this).inflate(R.layout.left_talk_layout, contentView, false);
        TextView leftTV = leftView.findViewById(R.id.leftTV);
        leftTV.setText(msg);
        contentView.addView(leftView);
    }



}
